package com.example.easyscript.service;

import com.example.easyscript.bean.task.TaskBean;
import com.example.easyscript.enums.PictureEnum;
import com.example.easyscript.utils.ImageUtils;
import com.example.easyscript.utils.JnaUtils;
import com.example.easyscript.utils.RobotUtils;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.io.File;
import java.util.List;

@Service
public class SystemImageServive {

    final static private String TASK_SPACE = "E:\\easy_script\\src\\main\\resources\\picture\\workspace\\baseimage";

    //获取任务的运行空间
    private String getTaskWorkSpacePath(TaskBean taskBean){
        String stockPath = TASK_SPACE + "\\" + taskBean.getTaskName()+"_"+taskBean.getId() ;
        File file = new File(stockPath) ;
        if(!file.exists() && !file.isDirectory()){
            file.mkdir() ;
        }
        return stockPath ;
    }

    //获取当前任务的截图并返回存储路径
    public String getTaskCurrentCapture(TaskBean taskBean) {
        System.out.println("正在获取窗口 "+taskBean.getWindowsName()+"的截图！");
        String stockPath =  getTaskWorkSpacePath(taskBean) + "\\" + PictureEnum.TASK_BACKGROUND;
        Rectangle rect = JnaUtils.getRect(taskBean.getWindowsName());
        RobotUtils.getCapture(rect,stockPath) ;
        return stockPath ;
    }

    public String getAllScreenPicture(TaskBean taskBean){
        System.out.println("正在获取当前计算机的全屏截图！");
        String stockPath =  getTaskWorkSpacePath(taskBean) + "\\" + PictureEnum.ALL_SCREEN_PICTURE;
        Rectangle rect = JnaUtils.getScreenSize();
        RobotUtils.getCapture(rect,stockPath) ;
        return stockPath ;
    }

    //对比多张模板图是否在原图当中
    public int checkTemplateIsInSource(List<String> templatesPath , String sourcePath){
        //模板通过数量
        int index = 0 ;
        int count = 0 ;
        System.out.println("开始验证----------------------------------");
        for (String item : templatesPath) {
            System.out.println(index + " templatesPath :"+item + "   "+"sourcePath :"+sourcePath);
            if(ImageUtils.judgeTemplateIsInSource(item,sourcePath)){
                count ++ ;
            }
            index++;
        }
        System.out.println("验证结束----------------------------------");
        return count ;
    }

}